package com.borqsprocessmanager.receiver;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.borqsprocessmanager.App;
import com.borqsprocessmanager.BatteryManagerService;
import com.borqsprocessmanager.BatteryManagerService.IStateReceiver;

public class TelephonChangedReceiver implements IStateReceiver {
    private final String TAG = "TelephonChangedReceiver";

    private BatteryManagerService mService;
    private TelephonyManager telephonyManager;

    private boolean isCall = false;

    public TelephonChangedReceiver(BatteryManagerService service) {
        this.mService = service;
        TelephonyManager telephonyManager = (TelephonyManager) service
                .getSystemService(Context.TELEPHONY_SERVICE);
        MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener();
        telephonyManager.listen(myPhoneStateListener,
                PhoneStateListener.LISTEN_CALL_STATE);
    }

    class MyPhoneStateListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                // 手机空闲起来了
                isCall = false;
                Log.v(TAG, "CALL_STATE_IDLE");
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                isCall = true;
                // " 手机铃声响了，来电号码
                Log.v(TAG, "CALL_STATE_RINGING");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                isCall = true;
                // " 电话被挂起了
                Log.v(TAG, "CALL_STATE_OFFHOOK");
                break;
            default:
                break;
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    }

    public boolean getState() {
        com.borqsprocessmanager.util.Log.v(TAG, "isCall===" + isCall);
        return !isCall;
    }

    public void initState() {
        // isCalled = false;
    }
}
